<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\BaseController;
use app\model\User as UserModel;

class User extends BaseController
{
    protected function initialize()
    {
        parent::initialize();
        // 检查用户登录状态
        $token = input('token');
        $user = UserModel::where('token', $token)->find();
        if (!$user) {
            return json(['code' => 401, 'msg' => '请先登录']);
        }
        $this->user = $user;
    }
    
    /**
     * 获取用户信息
     */
    public function info()
    {
        return json(['code' => 0, 'data' => $this->user]);
    }
    
    /**
     * 修改手机号
     */
    public function updatePhone()
    {
        $phone = input('post.phone');
        if (!$phone) {
            return json(['code' => 1, 'msg' => '手机号不能为空']);
        }
        
        $this->user->phone = $phone;
        $this->user->save();
        
        return json(['code' => 0, 'msg' => '修改成功']);
    }
} 